cmake_minimum_required(VERSION 3.14)
project(aicpu_ascend_engine)
if (BUILD_OPEN_PROJECT)
    get_filename_component(TOP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../.." ABSOLUTE)
    set(PROTO_PATH ${TOP_DIR}/third_party/metadef)
else()
    get_filename_component(TOP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../.." ABSOLUTE)
    set(PROTO_PATH ${TOP_DIR}/metadef)
endif()

set(BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

set(proto_src_files
    ${PROTO_PATH}/proto/task.proto
)

set(proto_nodedef_src_files
    ${CMAKE_CURRENT_SOURCE_DIR}/proto/cpu_attr.proto
    ${CMAKE_CURRENT_SOURCE_DIR}/proto/cpu_node_def.proto
    ${CMAKE_CURRENT_SOURCE_DIR}/proto/cpu_tensor.proto
    ${CMAKE_CURRENT_SOURCE_DIR}/proto/cpu_tensor_shape.proto
)

protobuf_generate(aicpu PROTO_SRCS PROTO_HDRS ${proto_src_files})
protobuf_generate(aicpu PROTO_NODEDEF_SRCS PROTO_NODEDEF_HDRS ${proto_nodedef_src_files})

set(local_lib_src_files
    ${CMAKE_CURRENT_SOURCE_DIR}/engine/aicpu_engine.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/kernel_info/aicpu_kernel_info.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/kernel_info/aicpu_cust_kernel_info.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/optimizer/aicpu_optimizer.cpp
    ${PROTO_HDRS}
    ${PROTO_NODEDEF_SRCS}
)

set(local_builder_src_files
    ${CMAKE_CURRENT_SOURCE_DIR}/kernel_builder/aicpu_kernel_builder.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/kernel_builder/aicpu_cust_kernel_builder.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/kernel_builder/aicpu_ascend_ops_kernel_builder.cpp
    ${PROTO_HDRS}
)

if (BUILD_OPEN_PROJECT)
  set(local_lib_inc_path
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${TOP_DIR}/compiler/graphcompiler/engines/cpueng
    ${TOP_DIR}/compiler/graphcompiler/engines/cpueng/common
    ${TOP_DIR}/third_party/metadef
    ${TOP_DIR}/third_party/metadef/inc
    ${TOP_DIR}/third_party/metadef/inc/external
    ${TOP_DIR}/third_party/inc/external
    ${TOP_DIR}/third_party/inc
    ${TOP_DIR}/third_party/inc/framework
    ${TOP_DIR}/third_party/inc/ops
    ${C_SEC_INCLUDE}
    ${JSON_INCLUDE}
    ${Protobuf_INCLUDE}
    ${CMAKE_BINARY_DIR}/proto/aicpu
    ${CMAKE_BINARY_DIR}/proto/aicpu/proto
  )
else()
  set (local_lib_inc_path
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${TOP_DIR}/air/compiler/graphcompiler/engines/cpueng
    ${TOP_DIR}/air/compiler/graphcompiler/engines/cpueng/common
    ${TOP_DIR}/inc
    ${TOP_DIR}/metadef/inc
    ${TOP_DIR}/graphengine/inc
    ${TOP_DIR}/inc/external
    ${TOP_DIR}/libc_sec/include
    ${TOP_DIR}/abl/libc_sec/include
    ${TOP_DIR}/metadef/inc/external
    ${TOP_DIR}/graphengine/inc/external
    ${TOP_DIR}/graphengine/inc/framework
    ${TOP_DIR}/third_party/json/include
    ${TOP_DIR}/open_source/json/include
    ${TOP_DIR}/metadef/graph
    ${TOP_DIR}/inc/aicpu/cpu_kernels
    ${TOP_DIR}/inc/external/aicpu
    ${CMAKE_BINARY_DIR}/proto/aicpu
    ${CMAKE_BINARY_DIR}/proto/aicpu/proto
  )
endif()

add_library(aicpu_ascend_engine SHARED
    ${local_lib_src_files}
)

add_library(atc_aicpu_ascend_engine SHARED
    ${local_lib_src_files}
)

add_library(aicpu_ascend_builder SHARED
    ${local_builder_src_files}
)

add_library(atc_aicpu_ascend_builder SHARED
    ${local_builder_src_files}
)

target_include_directories(aicpu_ascend_engine PRIVATE
    ${local_lib_inc_path}
)

target_include_directories(atc_aicpu_ascend_engine PRIVATE
    ${local_lib_inc_path}
)

target_include_directories(aicpu_ascend_builder PRIVATE
    ${local_lib_inc_path}
)

target_include_directories(atc_aicpu_ascend_builder PRIVATE
    ${local_lib_inc_path}
)

target_compile_options(aicpu_ascend_engine PRIVATE
    -Wno-deprecated-declarations
    -std=c++11
    -fno-common
    -fno-strict-aliasing
)

target_compile_options(atc_aicpu_ascend_engine PRIVATE
    -Wno-deprecated-declarations
    -std=c++11
    -fno-common
    -fno-strict-aliasing
)

target_compile_options(aicpu_ascend_builder PRIVATE
    -Wno-deprecated-declarations
    -std=c++11
   -fno-common
    -fno-strict-aliasing
)

target_compile_options(atc_aicpu_ascend_builder PRIVATE
    -Wno-deprecated-declarations
    -std=c++11
    -fno-common
    -fno-strict-aliasing
)

target_compile_definitions(aicpu_ascend_engine PRIVATE
    AICPU_PLUGIN
    google=ascend_private
)

target_compile_definitions(atc_aicpu_ascend_engine PRIVATE
    AICPU_PLUGIN
    google=ascend_private
)

target_compile_definitions(aicpu_ascend_builder PRIVATE
    AICPU_PLUGIN
    google=ascend_private
)

target_compile_definitions(atc_aicpu_ascend_builder PRIVATE
    AICPU_PLUGIN
    google=ascend_private
)

target_link_libraries(atc_aicpu_ascend_engine PRIVATE
    -Wl,--no-as-needed
    slog
    error_manager
    graph
    platform
    ascend_protobuf
    atc_aicpu_engine_common
    -Wl,--as-needed
    $<BUILD_INTERFACE:intf_pub>
     -lrt
    -ldl
)

target_link_libraries(aicpu_ascend_engine PRIVATE
    -Wl,--no-as-needed
    slog
    error_manager
    graph
    platform
    ascend_protobuf
    aicpu_engine_common
    -Wl,--as-needed
    -lrt
    -ldl
    $<BUILD_INTERFACE:intf_pub>
)

target_link_libraries(aicpu_ascend_builder PRIVATE
    -Wl,--no-as-needed
    slog
    error_manager
    graph
    platform
    ascend_protobuf
    aicpu_engine_common
    aicpu_ascend_engine
    register
    -Wl,--as-needed
    -lrt
    -ldl
    $<BUILD_INTERFACE:intf_pub>
)
target_link_libraries(atc_aicpu_ascend_builder PRIVATE
    -Wl,--no-as-needed
    slog
    error_manager
    graph
    platform
    ascend_protobuf
    register
    atc_aicpu_engine_common
    atc_aicpu_ascend_engine
    -Wl,--as-needed
    -lrt
    -ldl
    $<BUILD_INTERFACE:intf_pub>

)

set(INSTALL_LIBRARY_DIR lib)
#####改名称用的，因为一个cmake不能2个相同的名字的链接so 可执行程序 等
set(atc_aicpu_ascend_engine ${CMAKE_CURRENT_BINARY_DIR}/libatc_aicpu_ascend_engine.so)
set(atc_aicpu_ascend_builder ${CMAKE_CURRENT_BINARY_DIR}/libatc_aicpu_ascend_builder.so)

install(TARGETS aicpu_ascend_engine OPTIONAL
    EXPORT aicpu_ascend_engine-targets
    LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}
)

install(TARGETS atc_aicpu_ascend_engine OPTIONAL
    EXPORT atc_aicpu_ascend_engine-targets
    LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}/atclib
)

install(TARGETS aicpu_ascend_builder OPTIONAL
    EXPORT aicpu_ascend_builder-targets
    LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}
)

install(TARGETS atc_aicpu_ascend_builder OPTIONAL
    EXPORT atc_aicpu_ascend_builder-targets
    LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}/atclib
)

set_target_properties(atc_aicpu_ascend_engine
    PROPERTIES
    OUTPUT_NAME aicpu_ascend_engine
	LIBRARY_OUTPUT_DIRECTORY ${INSTALL_LIBRARY_DIR}/atclib
)

set_target_properties(atc_aicpu_ascend_builder
    PROPERTIES
    OUTPUT_NAME aicpu_ascend_builder
	LIBRARY_OUTPUT_DIRECTORY ${INSTALL_LIBRARY_DIR}/atclib
)

